package com.zjs.controller;

import com.github.pagehelper.PageInfo;
import com.zjs.common.bean.PageQuery;
import com.zjs.common.bean.ResponseResult;
import com.zjs.pojo.SysAuthFunction;
import com.zjs.pojo.SysUser;
import com.zjs.service.BcSysUserServiceI;
import com.zjs.service.SysAuthFunctionI;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @类名： SysAuthFunctionController
 * @说明： TODO
 * @作者：SS 苏帅出品
 * @时间：2019/12/14 16:06
 * @版本：1.0
 */
@Controller
@RequestMapping(value = "fun")
public class SysAuthFunctionController {



    @Autowired
    SysAuthFunctionI sysAuthFunctionI;


    @Autowired
    BcSysUserServiceI sysUserServiceI;
    //查询全部的功能权限

    @RequestMapping(value = "findAllPage.action")
    @ResponseBody
    public Map<String,Object> findAll(PageQuery query){

        Map<String,Object> map=new HashMap<>();


        PageInfo<SysAuthFunction> allFun = sysAuthFunctionI.findAllFun(query);

        map.put("total",allFun.getTotal());
        map.put("rows",allFun.getList());


        return map;
    }

    //找到添加页面
    @RequestMapping(value = "findAddPage.action")
    public String findAddPage(){

        return "admin/function_add";
    }


    //查找出全部的节点
    @RequestMapping(value = "findIdAndName.action")
    @ResponseBody
    public List<SysAuthFunction> findAllIdAndName(){

        return sysAuthFunctionI.findAllIdAndName();

    }


    //添加节点
    @RequestMapping(value = "saveFunction.action")
    @ResponseBody
    public ResponseResult saveFunction(SysAuthFunction function){

        int i = sysAuthFunctionI.saveFunction(function);

        if (i>0){

            return new ResponseResult(true,null);
        }


        return new ResponseResult(false,null);
    }


    //通过User Id查询具体的功能权限
    @RequestMapping(value = "findFunctionByUserId.action")
    @ResponseBody
    public List<SysAuthFunction> findFunByUserId(){

        //得到userId
        Subject subject = SecurityUtils.getSubject();

        SysUser sysUser = (SysUser) subject.getPrincipal();



        //通过userId 从中间表得到全部的 roleId
        String[] roleId = sysUserServiceI.findRoleId(sysUser.getId());

        //通过roleId得到全部的功能权限Id

        String[] funIdByRoleIs = sysAuthFunctionI.findFunIdByRoleIs(roleId);


        ArrayList<SysAuthFunction> sysAuthFunctions = new ArrayList<>();


        for (String funId:funIdByRoleIs){

            SysAuthFunction funByIds = sysAuthFunctionI.findFunByIds(funId);

            sysAuthFunctions.add(funByIds);

        }


        System.out.println(sysAuthFunctions);


        return sysAuthFunctions;
    }
}
